﻿<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%option explicit%>
<%
Dim RZ_IN
RZ_IN=1
%>
<!--#include file="../conn.inc.asp" -->
<!--#include file="admin_inc/admin.function.asp" -->
<!--#include file="admin_inc/admin.sqlfunction.asp" -->
<%
'作者：山林客（ah_bill）
'博客：http://ruizhinet.blog.163.com
'网站：http://www.ruizhinet.cn
'本信息不会影响您网站的正常访问，请保留
checkadmin(4)
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库管理</title>
<link type="text/css" href="style/style.css" rel="stylesheet" rev="stylesheet"/>
<script type="text/javascript" src="admin_js/common.js"></script>
</head>
<body>
<br>
<div class="addrecord">
<ul class="childtypelist">
<li><a href="?Action=DataBackup">备份数据库</a></li>
<li><a href="?Action=DataCompact">压缩数据库</a></li>
<li><a href="?Action=DataResume">恢复数据库</a></li>
<li><a href="?Action=DataDel">删除备份数据库</a></li>
</ul>
</div>
<br/>
<%
dim dotpos,database_dir,sysdatabase_name,ext
dotpos=InstrRev(RZ_DATABASE_PATH,"/")
database_dir=left(RZ_DATABASE_PATH,dotpos-1)
sysdatabase_name=right(RZ_DATABASE_PATH,len(RZ_DATABASE_PATH)-dotpos)

dim appPath
appPath=replace( LCase(Request.ServerVariables("PATH_INFO")), "admin/manage_database.asp","" )
If Left(RZ_DATABASE_PATH,1)="/" Then
	database_dir = database_dir&"/"
Else
	database_dir = appPath&database_dir&"/"
End If
database_dir=replace(database_dir,"//","/")
function CheckBackupDatabase(name)
	ext=right(name,(len(name)-InstrRev(name,".")))
	if ext="asp" and name<>sysdatabase_name then
		CheckBackupDatabase=true
	else
		CheckBackupDatabase=false
	end if
end function
Dim Action
Action=HTMLEncode(request.QueryString("Action"))
Select Case Action
Case "DataBackup"
  Call DataBackup
Case "DataCompact"
  Call DataCompact
Case "DataResume"
  Call DataResume
Case "DataDel"
  Call DataDel
Case "SaveDataBackup"
	SaveDataBackup
Case "SaveDataCompact"
	SaveDataCompact
Case "SaveDataReseum"
	SaveDataReseum
Case "SaveDataDel"
	SaveDataDel
Case Else
  Call DataBackup
End Select
%>
<%Sub DataBackup%>
<form id="DataBackup" name="DataBackup" method="post" action="manage_database.asp?Action=SaveDataBackup">
<table  cellspacing="0" cellpadding="0" class="table_500">
<tr>
	<th>备份数据（需要FSO支持）</th>
</tr>
<tr>
<td class="tdcenter">本操作会生成名称为“#backup-时间-随机数.asp”的备份文件<br/>
<input type="submit" value="开始备份" class="btn">
</td>
</tr>
</table>
</form>
<%End Sub%>
<%Sub DataCompact%>
<form id="Compact" name="Compact" method="post" action="manage_database.asp?Action=SaveDataCompact" onsubmit="return checkselect('datafilename')">
<table  cellspacing="0" cellpadding="0" class="table_500">
<tr>
	<th colspan="3">压缩数据（只能压缩备份数据库）</th>
</tr>
<tr>
	<td colspan="3">如何压缩当前系统数据库？<br/>①备份当前数据库；<br/>②压缩刚备份的数据库；<br/>③利用数据恢复，将刚才已经压缩的备份数据库覆盖当前系统数据库<br/><span style="color:red">警告：本操作存在一定风险，请谨慎使用！</span></td>
</tr>
<%
dim button_enable,fso,databasefolder,databasefile
button_enable=" disabled"
set fso=server.CreateObject("scripting.filesystemobject")
on error resume next
Set databasefolder=fso.Getfolder(Server.Mappath(database_dir))
for Each databasefile In databasefolder.files
if CheckBackupDatabase(databasefile.name) then
button_enable=""
%>
<tr>
<td><input type="checkbox" value="<%=databasefile.name%>" name="datafilename"></td>
<td><%=databasefile.name%></td>
<td><%=FormatNumber(databasefile.size/1024,2)%>K</td>
</tr>
<%
end if
next
set databasefolder=nothing
set fso=nothing
%>
<tr>
	<td colspan="3" class="tdcenter">
        <input type="submit" value="开始压缩" class="btn" <%=button_enable%>>
	</td>
</tr>
</table>
</form>
<%End Sub%>
<%Sub DataResume%>
<form id="DataBackup" name="DataBackup" method="post" action="manage_database.asp?Action=SaveDataReseum" onsubmit="return checkselect('datafilename')&&confirm('确定要恢复吗？本操作会覆盖当前系统数据库且操作不可逆');">
<table  cellspacing="0" cellpadding="0" class="table_500">
<tr>
	<th colspan="2">恢复数据(请选择要从哪个数据库恢复) </th>
</tr>
<%
dim button_enable,fso,databasefolder,databasefile
button_enable=" disabled"
set fso=server.CreateObject("scripting.filesystemobject")
on error resume next
Set databasefolder=fso.Getfolder(Server.Mappath(database_dir))
for Each databasefile In databasefolder.files
if CheckBackupDatabase(databasefile.name) then
button_enable=""
%>
<tr>
<td><input type="radio" value="<%=databasefile.name%>" name="datafilename"></td>
<td><%=databasefile.name%></td>
</tr>
<%
end if
next
set databasefolder=nothing
set fso=nothing
%>
<tr>
<td colspan="2" class="tdcenter">
<input type="submit" value="开始恢复" class="btn" <%=button_enable%>>
</td>
</tr>
</table>
</form>
<%End Sub%>
<%Sub DataDel%>
<form id="Compact" name="Compact" method="post" action="manage_database.asp?Action=SaveDataDel" onsubmit="return checkselect('datafilename')&&confirm('确定要删除吗？');">
<table  cellspacing="0" cellpadding="0" class="table_500">
<tr>
	<th colspan="2">删除备份数据库(请选择要删除的数据库) </th>
</tr>
<%
dim button_enable,fso,databasefolder,databasefile
button_enable=" disabled"
set fso=server.CreateObject("scripting.filesystemobject")
on error resume next
Set databasefolder=fso.Getfolder(Server.Mappath(database_dir))
for Each databasefile In databasefolder.files
if CheckBackupDatabase(databasefile.name) then
button_enable=""
%>
<tr>
<td><input type="checkbox" value="<%=databasefile.name%>" name="datafilename"></td>
<td><%=databasefile.name%></td>
</tr>
<%
end if
next
set databasefolder=nothing
set fso=nothing
%>
<tr>
</tr>
<tr>
	<td colspan="2" class="tdcenter">
        <input type="submit" value="开始删除" class="btn" <%=button_enable%>>
	</td>
</tr>
</table>
</form>
<%End Sub%>
<%
function SaveDataBackup()
	dim rd,nowtime,backupdatabasename,Fso
	rd=gen_key(13)
	nowtime=now()
	backupdatabasename="#backup-"&year(nowtime)&"-"&month(nowtime)&"-"&day(nowtime)&"-"&hour(nowtime)&"-"&minute(nowtime)&"-"&second(nowtime)&"-"&rd&".asp"
	set Fso=Server.CreateObject("Scripting.FileSystemObject")
	Fso.CopyFile Server.MapPath(database_dir&sysdatabase_name),Server.MapPath(database_dir&backupdatabasename)
		WriteResult("时间："&nowtime&"<br/>成功备份数据到"&backupdatabasename&"")
	Set Fso=nothing
end function
function SaveDataCompact()
	dim datafilenames,arr_datafilename,i,absolute_datafile,Fso,Engine,resultstring
	datafilenames=trim(request.Form("datafilename"))
	arr_datafilename=split(datafilenames,",")
	for i=0 to UBound(arr_datafilename)
		absolute_datafile=Server.MapPath(database_dir&trim(arr_datafilename(i)))
		set Fso=Server.CreateObject("Scripting.FileSystemObject")
		if Fso.FileExists(absolute_datafile) then
		  Set Engine =Server.CreateObject("JRO.JetEngine")
		  Engine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & absolute_datafile, _
									"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & absolute_datafile & "_temp.asp"
		  Fso.CopyFile absolute_datafile & "_temp.asp",absolute_datafile
		  Fso.DeleteFile(absolute_datafile & "_temp.asp")
		  resultstring=resultstring+"数据库："&trim(arr_datafilename(i))&"压缩成功<br/>"
		  set Engine = nothing
		else
		  resultstring=resultstring+"数据库："&trim(arr_datafilename(i))&"<span style='color:red;'>压缩失败</span><br/>"
		end if
		set Fso = nothing
	next
	WriteResult(resultstring)
end Function
function SaveDataReseum()
	dim datafilename,absolute_backupfile,absolute_sysfile,Fso
	datafilename=HTMLEncode(request.Form("datafilename"))
	absolute_backupfile=Server.MapPath(database_dir&datafilename)
	absolute_sysfile=Server.MapPath(database_dir&sysdatabase_name)
    set Fso=Server.CreateObject("Scripting.FileSystemObject")
    if Fso.FileExists(absolute_backupfile) then
      Fso.CopyFile absolute_backupfile,absolute_sysfile
      WriteResult("成功恢复数据库:<br/>"&absolute_backupfile&"->"&absolute_sysfile)
    else
      WriteResult("数据库恢复失败，请检查数据库路径是否有误")
    end if
	Set Fso=nothing
end Function
function SaveDataDel()
	dim datafilenames,arr_datafilename,i,absolute_datafile,Fso,resultstring
	datafilenames=trim(request.Form("datafilename"))
	arr_datafilename=split(datafilenames,",")
	for i=0 to UBound(arr_datafilename)
		absolute_datafile=Server.MapPath(database_dir&trim(arr_datafilename(i)))
		set Fso=Server.CreateObject("Scripting.FileSystemObject")
		If Fso.Fileexists(absolute_datafile) and trim(arr_datafilename(i))<>sysdatabase_name Then
			Fso.Deletefile(absolute_datafile)
			resultstring=resultstring+"数据库:"&trim(arr_datafilename(i))&"删除成功<br/>"
		else
			resultstring=resultstring+"数据库:"&trim(arr_datafilename(i))&"<span style='color:red;'>删除失败，请重新操作</span><br/>"
		end if
		Set Fso=nothing
	next
	WriteResult(resultstring)
end Function
%>
<%
Function WriteResult(str)
%>
<table  cellspacing="0" cellpadding="0" class="table_500">
<tr>
	<th>操作结果</th>
</tr>
<tr>
	<td style="padding-left:5px;"><%=str%></td>
</tr>
</table>
<%
End Function
%>
</body>
</html>